package org.apache.jsp.ForumHtml;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import clientData.MemberDataInt;
import java.util.LinkedList;
import org.w3c.dom.Document;
import java.sql.*;
import org.apache.jasper.tagplugins.jstl.core.Import;
import java.rmi.*;
import server.ForumHandlerInt;
import java.rmi.Naming;
import server.ForumHandlerInt;

public final class friends_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.List<java.lang.String> _jspx_dependants;

  static {
    _jspx_dependants = new java.util.ArrayList<java.lang.String>(1);
    _jspx_dependants.add("/ForumHtml/serverConnection.jsp");
  }

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.List<java.lang.String> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  public void _jspDestroy() {
  }

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html; charset=utf-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			"", true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");

	
   String serverDomain = "localhost";
   ForumHandlerInt forum = null;
  
     try
     {
   	 forum =  (ForumHandlerInt)Naming.lookup("rmi://"+serverDomain+"/ForumService");
	   }
   catch (Exception e){
	   
	   
	   response.sendError(response.SC_INTERNAL_SERVER_ERROR,"No connection to the forum server.");
	   response.flushBuffer();
		return;
   }

      out.write("\r\n");
      out.write("\r\n");
      out.write('\r');
      out.write('\n');

	
	String userName=(String) session.getAttribute("userName");
	String password=(String) session.getAttribute("password");
	if((userName==null)||(password==null)){
		response.sendRedirect("loggin.jsp");
		response.flushBuffer();
		return;
	}
	

      out.write("\r\n");
      out.write("\r\n");
      out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n");
      out.write("\r\n");
      out.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n");
      out.write("<head>\r\n");
      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n");
      out.write("<title>friends management</title>\r\n");
      out.write("<link href=\"screenStyle.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
      out.write("<link href=\"SpryAssets/SpryMenuBarVertical.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
      out.write("<script src=\"SpryAssets/SpryMenuBar.js\" type=\"text/javascript\"></script>\r\n");
      out.write("<script type=\"text/javascript\"> var MenuBar1 = new Spry.Widget.MenuBar(\"MenuBar1\", {imgRight:\"SpryAssets/SpryMenuBarRightHover.gif\"}); </script>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<script type=\"text/javascript\">\r\n");
      out.write("\t    \tfunction addFriends(){\r\n");
      out.write("\t\t\t\t// get the friend the user wants to add.\r\n");
      out.write("\t\t\t\tvar selectedIndex = document.getElementById(\"usersList\").selectedIndex;\r\n");
      out.write("\t\t\t\tif (selectedIndex == -1) {\r\n");
      out.write("\t\t\t\t\talert(\"please choose a friend to add\");\r\n");
      out.write("\t\t\t\t}\r\n");
      out.write("\t\t\t\telse {\r\n");
      out.write("\t\t\t\t\tvar friendUserName = document.getElementById(\"usersList\").options[selectedIndex].value;\r\n");
      out.write("\t\t\t\t\tdocument.getElementById(\"friendsListDiv\").innerHTML =\"\";\r\n");
      out.write("\t\t\t\t\t// \tadding the friend to user friends list\r\n");
      out.write("\t\t\t\t\tvar xmlhttp;\r\n");
      out.write("\t\t\t\t\tif (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari\r\n");
      out.write("\t\t\t\t\t\txmlhttp = new XMLHttpRequest();\r\n");
      out.write("\t\t\t\t\t} else {// code for IE6, IE5\r\n");
      out.write("\t\t\t\t\t\txmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");\r\n");
      out.write("\t\t\t\t\t}\r\n");
      out.write("\t\t\t\t\txmlhttp.onreadystatechange = function() {\r\n");
      out.write("\t\t\t\t\t\tif (xmlhttp.readyState == 4 && xmlhttp.status == 200) {\t\r\n");
      out.write("\t\t\t\t\t\t\tdocument.getElementById(\"friendsListDiv\").innerHTML = xmlhttp.responseText;\r\n");
      out.write("\t\t\t\t\t\t}\r\n");
      out.write("\t\t\t\t\t};\r\n");
      out.write("\t\t\t\t\txmlhttp.open(\"GET\", \"addFriend.jsp?friend=\" + friendUserName, true);\r\n");
      out.write("\t\t\t\t\txmlhttp.send();\r\n");
      out.write("\t\t\t\t}\t\r\n");
      out.write("\t\t\t}\r\n");
      out.write("\t\t</script>\r\n");
      out.write("<script type=\"text/javascript\">\r\n");
      out.write("\t    \r\n");
      out.write("\t\tfunction rmvFriend(){\r\n");
      out.write("\t\t\t// get the username to remove from friends list.\r\n");
      out.write("\t\t\tvar selectedIndex = document.getElementById(\"friendsList\").selectedIndex;\r\n");
      out.write("\t\t\tif (selectedIndex == -1) {\r\n");
      out.write("\t\t\t\talert(\"please choose a friend to remove\");\r\n");
      out.write("\t\t\t}\r\n");
      out.write("\t\t\telse {\r\n");
      out.write("\t\t\t\t// get the username to remove from friend list\r\n");
      out.write("\t\t\t\tvar friendUserName = document.getElementById(\"friendsList\").options[selectedIndex].value;\r\n");
      out.write("\t\t\t\tvar friendsListDiv = document.getElementById(\"friendsListDiv\");\r\n");
      out.write("\t\t\t\tfriendsListDiv.innerHTML =\"\";\r\n");
      out.write("\t\t\t\t// \tremoving the user from the friends list\r\n");
      out.write("\t\t\t\tvar xmlhttp;\r\n");
      out.write("\t\t\t\tif (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari\r\n");
      out.write("\t\t\t\t\txmlhttp = new XMLHttpRequest();\r\n");
      out.write("\t\t\t\t} else {// code for IE6, IE5\r\n");
      out.write("\t\t\t\t\txmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");\r\n");
      out.write("\t\t\t\t}\r\n");
      out.write("\t\t\t\txmlhttp.onreadystatechange = function() {\r\n");
      out.write("\t\t\t\t\tif (xmlhttp.readyState == 4 && xmlhttp.status == 200) {\t\r\n");
      out.write("\t\t\t\t\t\tfriendsListDiv.innerHTML = xmlhttp.responseText;\r\n");
      out.write("\t\t\t\t\t}\r\n");
      out.write("\t\t\t\t};\r\n");
      out.write("\t\t\t\txmlhttp.open(\"GET\", \"removeFriend.jsp?friendToRemove=\" + friendUserName, true);\r\n");
      out.write("\t\t\t\txmlhttp.send();\r\n");
      out.write("\t\t\t}\t\r\n");
      out.write("\t\t}\r\n");
      out.write("\t</script>\r\n");
      out.write("<script type=\"text/javascript\">\t\t\r\n");
      out.write("\t\tfunction getUsers(str) {\r\n");
      out.write("\t\t\tvar xmlhttp;\r\n");
      out.write("\t\t\tdocument.getElementById(\"usersListDiv\").innerHTML =\"\";\r\n");
      out.write("\t\t\tif (str.length == 0) {\r\n");
      out.write("\t\t\t\tdocument.getElementById(\"usersListDiv\").innerHTML = \"<select id=\\\"usersList\\\" name=\\\"usersList\\\" size=\\\"10\\\" ></select>\";\r\n");
      out.write("\t\t\t\treturn;\r\n");
      out.write("\t\t\t}\r\n");
      out.write("\t\t\tif (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari\r\n");
      out.write("\t\t\t\txmlhttp = new XMLHttpRequest();\r\n");
      out.write("\t\t\t} else {// code for IE6, IE5\r\n");
      out.write("\t\t\t\txmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");\r\n");
      out.write("\t\t\t}\r\n");
      out.write("\t\t\txmlhttp.onreadystatechange = function() {\r\n");
      out.write("\t\t\t\tif (xmlhttp.readyState == 4 && xmlhttp.status == 200) {\t\r\n");
      out.write("\t\t\t\t\tdocument.getElementById(\"usersListDiv\").innerHTML = xmlhttp.responseText;\r\n");
      out.write("\t\t\t\t}\r\n");
      out.write("\t\t\t};\r\n");
      out.write("\t\t\txmlhttp.open(\"GET\", \"getUsers.jsp?prefix=\" + str, true);\r\n");
      out.write("\t\t\txmlhttp.send();\r\n");
      out.write("\t\t}\r\n");
      out.write("\t\r\n");
      out.write("\t</script>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<!-- this script is notifing the user about his friends -->\r\n");
      out.write("\r\n");
      out.write("<script type=\"text/javascript\" src=\"jquery.js\"></script>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<style type=\"text/css\">\r\n");
      out.write("select {\r\n");
      out.write("\tfloat: left;\r\n");
      out.write("\tborder-top-style: groove;\r\n");
      out.write("\tborder-right-style: groove;\r\n");
      out.write("\tborder-bottom-style: groove;\r\n");
      out.write("\tborder-left-style: groove;\r\n");
      out.write("\tlist-style-type: none;\r\n");
      out.write("\tlist-style-image: none;\r\n");
      out.write("\twidth: 120px;\r\n");
      out.write("\theight: 200px;\r\n");
      out.write("}\r\n");
      out.write("</style>\r\n");
      out.write("</head>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<body>\r\n");
      out.write("\t<div class=\"pageTitle\">\r\n");
      out.write("\t\t<h1>FRIENDS MANAGEMENT</h1>\r\n");
      out.write("\t\t<hr />\r\n");
      out.write("\t</div>\r\n");
      out.write("\t\t\r\n");
      out.write("\t<ul id=\"MenuBar1\" class=\"MenuBarVertical\">\r\n");
      out.write("\t\t<li><a href=\"mainScreen.jsp\">main screen</a></li>\r\n");
      out.write("\t\t<li><a href=\"index.jsp\" onclick = \"logoutFunc()\">logout</a>  </li>\r\n");
      out.write("\t</ul>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\t<div id=\"friendsFormDiv\">\r\n");
      out.write("\t\t<form id=\"friendsForm\" method=\"get\" action=\"\">\r\n");
      out.write("\t\t\t<table id=\"friendTable\" width=\"429\" border=\"0\">\r\n");
      out.write("\t\t\t\t<tr>\r\n");
      out.write("\t\t\t\t\t<th align=\"left\" colspan=\"2\" scope=\"row\"><label> type user name prefix for searching user:</label>\r\n");
      out.write("\t\t\t\t\t</th>\r\n");
      out.write("\t\t\t\t</tr>\r\n");
      out.write("\t\t\t\t<tr>\r\n");
      out.write("\t\t\t\t\t<th align=\"left\" colspan=\"2\" scope=\"row\"><input name=\"userPrefix\"\r\n");
      out.write("\t\t\t\t\t\ttype=\"text\" onkeyup=\"getUsers(this.value)\" />\r\n");
      out.write("\t\t\t\t\t</th>\r\n");
      out.write("\t\t\t\t</tr>\r\n");
      out.write("\t\t\t\t<tr>\r\n");
      out.write("\t\t\t\t\t<th align=\"left\" width=\"213\" scope=\"row\"><label> users found</label></th>\r\n");
      out.write("\t\t\t\t\t<th align=\"left\" width=\"206\">friends list</th>\r\n");
      out.write("\t\t\t\t</tr>\r\n");
      out.write("\t\t\t\t<tr>\r\n");
      out.write("\t\t\t\t\t<th scope=\"row\">\r\n");
      out.write("\t\t\t\t\t\t<div id=\"usersListDiv\">\r\n");
      out.write("\t\t\t\t\t\t\t<select id=\"usersList\" name=\"usersList\" size=\"10\"></select>\r\n");
      out.write("\t\t\t\t\t\t</div>\r\n");
      out.write("\t\t\t\t\t</th>\r\n");
      out.write("\t\t\t\t\t<td>\r\n");
      out.write("\t\t\t\t\t\t<div id=\"friendsListDiv\">\r\n");
      out.write("\t\t\t\t\t\t\t<select name=\"friendsList\" size=\"10\" id=\"friendsList\">\r\n");
      out.write("\t\t\t\t\t\t\t\t");

							LinkedList<MemberDataInt> friends = forum.getUserFriends(userName,password); 
							for(MemberDataInt friend : friends){	
									
      out.write("<option value=\"");
      out.print(friend.getUserName());
      out.write('"');
      out.write('>');
      out.print(friend.getUserName());
      out.write("</option>\r\n");
      out.write("\t\t\t\t\t\t\t\t");

							}
							
      out.write("\r\n");
      out.write("\t\t\t\t\t\t\t</select>\r\n");
      out.write("\t\t\t\t\t\t</div></td>\r\n");
      out.write("\t\t\t\t</tr>\r\n");
      out.write("\t\t\t\t<tr>\r\n");
      out.write("\t\t\t\t\t<td><input name=\"addFriend\" type=\"button\" value=\"add friend\" onclick=\"addFriends()\" /></td>\r\n");
      out.write("\t\t\t\t\t<td><input name=\"removeFriend\" type=\"button\" value=\"remove friend\" onclick=\"rmvFriend()\" /></td>\r\n");
      out.write("\t\t\t\t</tr>\r\n");
      out.write("\t\t\t</table>\r\n");
      out.write("\t\t</form>\r\n");
      out.write("\t</div>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\t\r\n");
      out.write("\t\r\n");
      out.write("\t\r\n");
      out.write("</body>\r\n");
      out.write("</html>\r\n");
      out.write("\r\n");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
